# -*- coding:utf-8 -*-
# File Name:redis.py
# Author:jing
# Date:2018/12/19
from ygframe.util.date_util import get_date

from interface.resource import Resource
from model.mongo_model.resource_info import ResourceInfo
from util.port_util import get_possible_port
from util.str_util import get_random_string, get_random_kube_name

__author__ = 'jing'


class Redis(Resource):
    def after_release_kube(self):
        # todo clean persistence files
        pass

    @classmethod
    def get_resource_type(cls):
        return 'redis'

    def get_access_info(self):
        info = super().get_access_info()
        info['password'] = ''
        return info

    def get_config(self):
        return {
            'requests': self.resource_info.requests,
            'limits': self.resource_info.limits
        }

    @classmethod
    def create_info(cls, resource_data):
        uid = resource_data.get('uid')
        container_port = 6379
        cmd = ['redis-server']
        password = get_random_string(10)
        args = [
            '--requirepass {}'.format(password),
            '--bind 0.0.0.0'
        ]

        info = ResourceInfo(
            apply_date=get_date(),
            container_port=container_port,
            image=resource_data.get('image') or 'redis:4.0',
            user_id=uid,
            host_port=get_possible_port(),
            resource_type=cls.get_resource_type(),
            args=args,
            env={'password': password},
            cmd=cmd,
            resource_name=resource_data.get('resource_name'),
            limits=resource_data.get('limits'),
            requests=resource_data.get('requests')
        )

        cls.save_resource_info(info)

    def check(self) -> bool:
        # todo ping
        return True
